vm = new Vue({
    el: '#app',  //选中整个main
    data: {
        orderList: {},
        user:'' ,
        totalMoney: 0,
        hotGoodsList: {},
        types: {},
        goodsName: ''
    },
    watch: {},
    methods: {
        exitLogin:function(){
            axios.get("user/exitLogin").then(rs=>{
                sessionStorage.removeItem("user");
                sessionStorage.removeItem("showNum");
                sessionStorage.removeItem("typeId");
                layer.msg("退出登录成功！");
                location.href="index.html";
            });
        },
        //搜索，模糊查询
        search: function () {
            sessionStorage.setItem("typeId", 0);
            sessionStorage.setItem("goodsName", this.goodsName);
            location.href = 'productList.html';
        },
        //列表转换
        toGoodsListByType: function (typeId) {
            sessionStorage.setItem("typeId", typeId);
            sessionStorage.removeItem("goodsName");
            location.href = 'productList.html';
        },
        //获取商品列表
        getAllGoodsType: function () {
            axios.get('type/getAllGoodsType').then(rs => {
                this.types = rs.data.obj;
            });
        },
        // toPayOder/{orderId}
        //全部结算
        toPayAllOders: function () {
            for (let i = 0; i < this.orderList.length; i++) {
                this.toPayOder(this.orderList[i].orderId);
            }
        },
        //去支付单个订单
        toPayOder: function (orderId) {
            axios.put(`order/toPayOder/${orderId}`).then(rs => {
                layer.msg(rs.data.msg);
                location.href = 'waitPay.html';
            });
        },
        deleteOderByOrderId: function (orderId) {
            layer.msg("是否要删除该商品？", {
                time: 0,      //表示不自动关闭
                btn: ['是', '否'],
                yes: (index) => {
                    axios.delete(`order/deleteOderByOrderId/${orderId}`).then(rs => {
                        this.getGoodsListByUserId();
                        layer.msg(rs.data.msg);
                    });
                }
            })
        },
        getGoodsListByUserId: function () {
            this.totalMoney = 0;
            axios.get(`order/getGoodsListByUserId/${this.user.id}`).then(rs => {
                this.orderList = rs.data.obj;
                for (let i = 0; i < this.orderList.length; i++) {
                    this.totalMoney += this.orderList[i].goodsCount * this.orderList[i].goods.price;
                }
            })
        },
        add: function (orderId, price) {
            this.totalMoney = 0;
            for (let i = 0; i < this.orderList.length; i++) {
                if (orderId == this.orderList[i].orderId) {
                    this.orderList[i].goodsCount++;
                    // addGoodCount
                    axios.put(`order/addGoodCount/${orderId}/${this.orderList[i].goodsCount}/${price}`)
                        .then(rs => {
                        });
                }
                this.totalMoney += this.orderList[i].goodsCount * this.orderList[i].goods.price;
            }
        },
        reduce: function (orderId, price) {
            this.totalMoney = 0;
            for (let i = 0; i < this.orderList.length; i++) {
                if (orderId == this.orderList[i].orderId) {
                    if (this.orderList[i].goodsCount > 1) {
                        this.orderList[i].goodsCount--;
                        axios.put(`order/reduceGoodCount/${orderId}/${this.orderList[i].goodsCount}/${price}`)
                            .then(rs => {
                            });
                    }
                }
                this.totalMoney += this.orderList[i].goodsCount * this.orderList[i].goods.price;
            }
            // reduceGoodCount
        },
        //商品详情
        toDetail: function (goods, event) {
            // event.preventDefault();
            axios.put(`goods/updateLookById/${goods.id}/${goods.look}`).then(rs => {
                goods.look = goods.look + 1;
                layer.msg(rs.data.msg);
                sessionStorage.setItem('goods', JSON.stringify(goods));
                location.href = 'goodsDetail.html';
            }).catch(e => {
                layer.msg(e.message)
            });
        },
        getGoods: function () {
            axios.get("goods/getHotGoods").then(rs => {
                this.hotGoodsList = rs.data.obj;
            })
        },
        getUserInfo:function () {
            // debugger;
            let user = sessionStorage.getItem("user");
            if (user==''||user==undefined||user==null) {
                axios.get('user/getUserInfo').then(rs => {
                    debugger;
                    let user = rs.data.obj;
                    sessionStorage.setItem("user", JSON.stringify(user));
                    sessionStorage.setItem("showNum", 5);
                    sessionStorage.setItem("typeId", 0);
                    location.href = 'home.html';
                }).catch(e => {
                    layer.msg(e.message);
                })
            }
            this.user=JSON.parse(sessionStorage.getItem("user"));
            this.defaultAddrId=JSON.parse(sessionStorage.getItem("user")).defaultAddrid;
        }
    },
    created: function () {
        this.getUserInfo();
        this.getGoodsListByUserId();
        this.getGoods();
        this.getAllGoodsType();
    }
});